package com.ccys.cms.controller.discount;

import com.ccys.cms.service.discount.PromoCodeAutomatedService;
import com.ccys.common.constants.TouristApi;
import com.ccys.common.vo.ResponseResult;
import com.ccys.db.dto.discount.MatchingPremiumCouponsDto;
import com.ccys.db.dto.discount.PromoCodeAutomatedDto;
import com.ccys.db.vo.discount.PromoCodeAutomatedMemberListVo;
import com.ccys.db.vo.discount.PromoCodeAutomatedPageQueryVo;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.text.ParseException;
import java.util.List;

/**
 * @program: WhiteCoat
 * @description: 高级优惠卷接口
 * @author: ZhangJingCan
 * @create: 2023-12-15 15:34
 **/

@RestController
@RequestMapping("discount")
@Api(tags = "discount-高级优惠卷接口")
public class PromoCodeAutomatedController {

    @Autowired
    private PromoCodeAutomatedService promoCodeAutomatedService;


    @PostMapping("promoCodeAutomated")
    @ApiOperation("保存高级优惠卷")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "对象id/修改必传", required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "名称", required = true, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "generateType", value = "0auto 1NRIC 2pre existing promo code", required = true, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "type", value = "0:free delivery(快递费用免费);1:fixed $ off bill(账单抵扣金额);2:% off bill(账单打折);3:fixed $ off consultation(问诊费用抵扣金额);4:% off consultation(问诊费用打折);5:fixed $ off delivery(快递费用抵扣金额);6:% off delivery(快递费用打折);7:%off consultation + % off delivery(问诊费用与快递费用同时打折)", required = true, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "emailMatchingPattern", value = "电子邮件匹配模式", required = true, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "status", value = "状态", required = true, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "prefix", value = "generateType不为2的时候这个字段必传", required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "generateLength", value = "生成的code长度字段，generate_type 为 2 时，存储-1", required = true, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "sendEmail", value = "sendEmail:0/1", required = true, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "receiver", value = "0self 1dependent", required = true, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "discountLabel", value = "discountLabel", required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "amount", value = "type不为0的时候，必须传", required = false, dataType = "double", paramType = "query"),
            @ApiImplicitParam(name = "limitNumber", value = "generateType不为2的时候必传", required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "endType", value = "generateType不为2的时候必传:0duration 1date", required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "endDuration", value = "(持续时长(天))endType为0必须传入", required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "endDate", value = "(到期时间)endType为1必须传入", required = false, dataType = "date", paramType = "query"),
            @ApiImplicitParam(name = "reusable", value = "generateType不为2的时候必传:同一个人是否可重复使用:0/1", required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "code", value = "generateType为2的时候必传:选择的普通优惠卷code", required = false, dataType = "string", paramType = "query"),
    })
    public ResponseResult savePromoCodeAutomated(@ApiIgnore PromoCodeAutomatedDto dto) {
        return promoCodeAutomatedService.savePromoCodeAutomated(dto);
    }


    @DeleteMapping("promoCodeAutomated")
    @ApiOperation("根据ID删除高级优惠卷")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "对象id", required = true, dataType = "string", paramType = "query"),
    })
    public ResponseResult deletePromoCodeAutomated(@ApiIgnore PromoCodeAutomatedDto dto) {
        return promoCodeAutomatedService.deletePromoCodeAutomated(dto);
    }


    @GetMapping("promoCodeAutomated")
    @ApiOperation("分页查询高级优惠卷")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "name", required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "type", value = "0:free delivery(快递费用免费);1:fixed $ off bill(账单抵扣金额);" +
                    "2:% off bill(账单打折);3:fixed $ off consultation(问诊费用抵扣金额);4:% off consultation(问诊费用打折);" +
                    "5:fixed $ off delivery(快递费用抵扣金额);6:% off delivery(快递费用打折);" +
                    "7:%off consultation + % off delivery(问诊费用与快递费用同时打折)/查询所有传null", required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "status", value = "0/1,查询所有传null", required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "startDate", value = "开始日期,格式:yyyy-MM-dd", required = false, dataType = "date", paramType = "query"),
            @ApiImplicitParam(name = "endDate", value = "结束日期,格式:yyyy-MM-dd", required = false, dataType = "date", paramType = "query"),
            @ApiImplicitParam(name = "orderByStr",
                    value = "(默认传：t1.created_on descend),(左到右依次:t1.name,t1.number,t1.type,t1.amount,t1.email_matching_pattern,limitUser,t1.end_date,t1.status,t2.user_fullname,t1.created_on)",
                    required = true, dataType = "string", paramType = "query"),
    })
    public ResponseResult<PageInfo<PromoCodeAutomatedPageQueryVo>, Object> findPagePromoCodeAutomated(@ApiIgnore PromoCodeAutomatedDto dto) {
        return promoCodeAutomatedService.findPagePromoCodeAutomated(dto);
    }


    @PutMapping("promoCodeAutomated/status")
    @ApiOperation("根据ID修改高级优惠卷状态")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "对象id", required = true, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "status", value = "状态：0-禁用，1-启用", required = true, dataType = "string", paramType = "query"),
    })
    public ResponseResult updatePromoCodeAutomatedStatus(@ApiIgnore PromoCodeAutomatedDto dto) {
        return promoCodeAutomatedService.updatePromoCodeAutomatedStatus(dto);
    }


    @GetMapping("promoCodeAutomated/findListPromoCodeAutomatedMemberByCodeId")
    @ApiOperation("根据高级优惠卷ID查询拥有的用户列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "对象id", required = true, dataType = "string", paramType = "query"),
    })
    public ResponseResult<List<PromoCodeAutomatedMemberListVo>, Object> findListPromoCodeAutomatedMemberByCodeId(@ApiIgnore PromoCodeAutomatedDto dto) {
        return promoCodeAutomatedService.findListPromoCodeAutomatedMemberByCodeId(dto);
    }


    @PostMapping("promoCodeAutomated/queryAvailablePremiumCouponsByEmail")
    @ApiOperation("匹配高级优惠卷并发送邮件")
    @TouristApi
    public void queryAvailablePremiumCouponsByEmail(@RequestBody MatchingPremiumCouponsDto dto) throws ParseException {
        promoCodeAutomatedService.queryAvailablePremiumCouponsByEmail(dto);
    }


}
